﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PluginMain
{
    public partial class InputDirsName : Form
    {
        //当前要添加的文件夹地址
        private string DirPath;
        private int Bit;
        private TreeNodeCollection _treeNodeCol;
        /// <summary>
        /// 初始的文件夹位置
        /// </summary>
        /// <param name="DirPath"></param>
        public InputDirsName(string DirPath,TreeNodeCollection treeNodeCol,int Bit)
        {
            InitializeComponent();
            this.Bit = Bit;
            this._treeNodeCol = treeNodeCol;
            if (Bit==1)
            {
                this.Text = "创建工程文件";
                lb_DirName.Text = "工程名:";
            }
            this.DirPath = DirPath;
        }

        private void btn_Accept_Click(object sender, EventArgs e)
        {
            if (Bit==1)
            {
                try
                {
                    string path = DirPath + tx_DirName.Text + ".prj";
                    if (File.Exists(path))
                    {
                        MessageBox.Show("已经存在项目!");
                    }
                    else
                    {
                        Project p = new Project(path);
                        p.Save();
                        TreeNode t = _treeNodeCol.Add(Path.GetFileName(path), Path.GetFileName(path), 1);
                        t.SelectedImageIndex = 1;
                        t.ImageIndex = 1;
                        
                        this.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
                //创建文件夹
            else if (Bit==0)
            {
                string path = DirPath + tx_DirName.Text;
                try
                {
                    if (Directory.Exists(path))
                    {
                        MessageBox.Show("已经存在文件夹!");
                        return;
                    }
                    Directory.CreateDirectory(path);
                    TreeNode t = _treeNodeCol.Add(Path.GetFileName(path), Path.GetFileName(path), 1);
                    t.SelectedImageIndex = 0;
                    t.ImageIndex = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
